//
//  YFMainViewController.swift
//  微博
//
//  Created by 余仿 on 16/4/6.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

let baseTag = 1000
class YFMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myTabBar = YFTabBar()

        myTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)

        setValue(myTabBar, forKey: "tabBar")

        addChildViewControllers()

    }

    func plusBtnDidClick() {

        YFComposeView().show(self)
    }


    //MARK:添加子控制器
    func addChildViewControllers() {

        addChildControllers(YFHomeTableViewController(), title: "首页", imageName: "tabbar_home",tag: 0)
        addChildControllers(YFMessageTableViewController(), title: "消息", imageName: "tabbar_message_center",tag: 1)
        addChildControllers(YFDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover",tag: 2)
        addChildControllers(YFProfileTableViewController(), title: "我", imageName: "tabbar_profile",tag: 3)
    }

    func addChildControllers(vc:UIViewController,title:String,imageName:String,tag:Int){


        vc.tabBarItem = YFTabBarItem()

        vc.title = title

        vc.tabBarItem.image = UIImage(named: imageName)

        vc.tabBarItem.tag = tag + baseTag

        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)//取消系统蓝色状态

        //设置文字的属性,如颜色,大小等
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)

        //tabBar.tintColor = UIColor.orangeColor()

        //快速给普通控制器设置导航控制器
        let nav = YFBasicViewController(rootViewController: vc)

        addChildViewController(nav)
    }

    //tabBarItem点击动画效果
    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

        print("选中了底部tabBar中的item")
        var index = 0
        for subView in tabBar.subviews{
            //需要点击的一个
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){

                if item.tag - baseTag == index{

                    for s in subView.subviews{
                        if s.isKindOfClass(NSClassFromString("UITabBarSwappableImageView")!){

                            UIView.animateWithDuration(0.1, animations: { () -> Void in

                                s.transform = CGAffineTransformMakeScale(0.5, 0.5)
                                }, completion: { (_) -> Void in

                                    UIView.animateWithDuration(0.3, animations: { () -> Void in
                                        s.transform = CGAffineTransformMakeScale(1, 1)
                                    })
                            })

                        }
                    }
                }
                index++
            }
            if subView.isKindOfClass(NSClassFromString("UIButton")!){
                print("找到加号了")
            }
        }
        
    }
    
    
}
